package com.itao.minio.client;

import com.itao.minio.util.MinioUtil;
import io.minio.*;
import io.minio.http.Method;

import java.io.File;
import java.io.FileInputStream;

/**
 * minio object操作
 *
 * @Author sjt
 * @CreateTime 2024/5/6 14:43:13
 */
public class ObjectTest {


    /**
     * 上传文件
     */
    public static void putObject() throws Exception {
        File file = new File("E:\\drools.log");
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket("test-minio")
                .object("drools.log")
                .stream(new FileInputStream(file), file.length(), -1)
                .build();
        ObjectWriteResponse response = MinioUtil.getMinioClient().putObject(putObjectArgs);
        System.out.println("object:" + response.object());
        System.out.println("bucket:" + response.bucket());
        System.out.println("headers:" + response.headers());
    }

    /**
     * 上传本地文件
     */
    public static void uploadObject() throws Exception {
        UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                .bucket("test-minio")
                .object("1/1.png")
                .filename("E:\\1.png")
                .build();
        ObjectWriteResponse response = MinioUtil.getMinioClient().uploadObject(uploadObjectArgs);
        System.out.println("object:" + response.object());
        System.out.println("bucket:" + response.bucket());
        System.out.println("headers:" + response.headers());
    }

    /**
     * 下载文件
     */
    public static void downloadObject() throws Exception {
        DownloadObjectArgs downloadObjectArgs = DownloadObjectArgs.builder()
                .bucket("test-minio")
                .object("drools1.log")
                .filename("E:\\drools1.log")
                .build();
        MinioUtil.getMinioClient().downloadObject(downloadObjectArgs);
        System.out.println("下载文件成功");
    }

    /**
     * 删除文件
     */
    public static void removeObject() throws Exception {
        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
                .bucket("test-minio")
                .object("drools1.log")
                .build();
        MinioUtil.getMinioClient().removeObject(removeObjectArgs);
        System.out.println("删除文件成功");
    }

    /**
     * 获取文件的url用于浏览器访问
     */
    public static void getPresignedObjectUrl() throws Exception {
        GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs.builder()
                .bucket("test-minio")
                .object("1.png")
                // 过期时间，默认7天
                // .expiry()
                .method(Method.GET)
                .build();
        String url = MinioUtil.getMinioClient().getPresignedObjectUrl(getPresignedObjectUrlArgs);
        System.out.println("url:" + url);
    }

    /**
     * 获取文件的状态
     */
    public static void statObject() throws Exception {
        StatObjectArgs statObjectArgs = StatObjectArgs.builder()
                .bucket("test-minio")
                .object("1.png")
                .build();
        StatObjectResponse response = MinioUtil.getMinioClient().statObject(statObjectArgs);
        System.out.println("object:" + response.object());
        System.out.println("bucket:" + response.bucket());
        System.out.println("headers:" + response.headers());
        System.out.println("deleteMarker:" + response.deleteMarker());
        System.out.println("contentType:" + response.contentType());
    }

    public static void main(String[] args) throws Exception {
        statObject();
    }
}
